package 力扣刷题.times;

import java.util.Arrays;
import java.util.Scanner;
class S{
    static int i;
    static {i=1;}
}
class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int jinwei1 = 0, jinwei2 = 0;

        while (true) {

            int sum = 0;

            if (l1 == null && l2 == null&&jinwei1==0)
                break;
             if (l1 != null) {
                sum += l1.val;
                l1=l1.next;
            }
             if (l2 != null) {
                sum += l2.val;
                l2=l2.next;
            }

            sum+=jinwei1;
             jinwei1=0;
            if (sum >= 10) {
                sum -= 10;
                jinwei2 = 1;
            }
            else jinwei2=0;
            if (head == null) {
                tail = new ListNode(sum);
                head = tail;
            } else {
                tail.next = new ListNode(sum);
                tail = tail.next;
            }

            jinwei1 = jinwei2;

        }
        return head;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        System.exit(-1);
        System.out.println(Arrays.deepToString(args));
ClassLoader.getSystemClassLoader().loadClass(S.class.getName());
        System.out.println(S.class.getName());
        System.out.println(S.i);
        System.out.println(System.setProperty("112","123"));
        System.out.println(System.getenv("11123"));
        ListNode l1 = createList(new int[]{1}),l2=createList(new int[]{9,9}) ;
        ListNode head=new Solution().addTwoNumbers(l1,l2);
        while(head!=null) {
            System.out.println(head.val);
            head=head.next;
        }
        Scanner s=new Scanner(System.in);

        s.nextInt();
    }

    public static ListNode createList(int[] a) {
        ListNode head = null, tail = null;
        for (int i = 0; i < a.length; i++) {
            if (head == null) {
                head = new ListNode(a[i]);
                tail = head;
            } else {
                tail.next = new ListNode(a[i]);
                tail = tail.next;
            }
        }
        return head;
    }
}